package com.wirelesspienetwork.overview.views;

import com.wirelesspienetwork.overview.misc.OverviewConfiguration;
import com.wirelesspienetwork.overview.misc.Rect;
import com.wirelesspienetwork.overview.model.OverviewAdapter;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.StackLayout;
import ohos.agp.render.Canvas;
import ohos.app.Context;

public class Overview extends StackLayout implements OverviewStackView.Callbacks, Component.DrawTask {

    public interface RecentsViewCallbacks {
        public void onCardDismissed(int position);
        public void onAllCardsDismissed();
    }

    OverviewStackView mStackView;
    OverviewConfiguration mConfig;
    OverviewAdapter mAdapter;
    RecentsViewCallbacks mCallbacks;

    public Overview(Context context) {
        super(context);
        init(context);
    }

    public Overview(Context context, AttrSet attrs) {
        super(context, attrs);
        init(context);
    }

    public Overview(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, "");
        init(context);
    }

    private void init(Context context) {
        mConfig = new OverviewConfiguration(context);
        addDrawTask(this);
    }

    /** Sets the callbacks
     * @param cb  */
    public void setCallbacks(RecentsViewCallbacks cb) {
        mCallbacks = cb;
    }

    /** Set/get the bsp root node
     * @param adapter  */
    public void setTaskStack(OverviewAdapter adapter) {
        if (mStackView != null) {
            removeComponent(mStackView);
        }
        mAdapter = adapter;
        mStackView = new OverviewStackView(getContext(), adapter, mConfig);
        LayoutConfig params = new LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT);
        mStackView.setLayoutConfig(params);
        mStackView.setCallbacks(this);
        mStackView.setAlpha(0);
        mStackView.createAnimatorProperty().alpha(1.f).setDelay(2000).setDuration(3500).start();
        addComponent(mStackView);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        onMeasure(getWidth(),getHeight());
    }

    /**
     * This is called with the full size of the window since we are handling our own insets.
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = EstimateSpec.getSize(widthMeasureSpec);
        int height = EstimateSpec.getSize(heightMeasureSpec);
        if (mStackView != null) {
            Rect stackBounds = new Rect();
            mConfig.getOverviewStackBounds(width, height, stackBounds);
            mStackView.setStackInsetRect(stackBounds);
        }
    }

    @Override
    public void onCardDismissed(int position) {
        if (mCallbacks != null) {
            mCallbacks.onCardDismissed(position);
        }
    }

    @Override
    public void onAllCardsDismissed() {
        if (mCallbacks != null) {
            mCallbacks.onAllCardsDismissed();
        }
    }
}
